// 03myCenter/02sectorization/01myChange/recharge/recharge.js
var co = require('../../../../resource/js/common.js')
var app = getApp()
var _this,_data

Page({
   /*页面的初始数据 */
   data: {
      isInput: 0,
      money: ''
   },

   /*生命周期函数--监听页面加载 */
   onLoad: function(options) {
      _this = this
      _data = this.data
   },
   // 输入充值金额
   money: function(e) {
      var mon = parseFloat(e.detail.value)
      var m
      if (co.empty(mon) && mon > 0) {
         m = 0
      } else {
         m = 1
      }
      this.setData({
         isInput: m,
         money: mon
      })
   },
   // 下一步
   next: function() {
      if (this.data.isInput == 1) {
         this.weXinPay(_data.money)
      }
   },
   // 微信支付
   weXinPay: function(money) {
      //账户充值
      var appid = app.Data.app.appid
      var param = {
         openid: app.Data.userInfo.Wx_Openid[appid],
         appid: appid,
         money: money,
         abstract: '账户充值'
      }
      co.getShareApi('api/Wxpay/createParam', param, function(res) {
         res.data.success = function(result) {
            _this.moneyRechargeConsume(100)
            _this.addUserMoneyRecord(param.money)
            console.log('【调起支付成功res】', result)
         }
         res.data.fail = function(result) {
            console.log('【调起支付失败res】', result)
         }
         //调起微信支付
         wx.requestPayment(res.data)
      })
   },
   //增加用户资金流水记录
   addUserMoneyRecord(money) {
      var uRecord = {
         User_ID: app.Data.userInfo.User_ID,
         Abstract: '充值',
         Payer: 'User_' + app.Data.userInfo.User_ID,
         Money: money,
         Pay_To: '91_pay',
         Pay_From: 'wx_pay',
         //To_Name: app.Data.userInfo.User_NickName
      }
      co.getJyhsApi('money/Recorduser/userAdd', uRecord, function(res) {
         if (res.code == 0) {
            app.Data.userInfo.User_Balance = res.data.New_Balance
            wx.setStorageSync('userInfo', app.Data.userInfo)
            co.modal('充值成功')
            wx.navigateBack({
               delta: 1
            })
         } else {
            co.modal('充值失败')
         }
      })
   },
   // 充值接口
   moneyRechargeConsume: function(money) {
      var param = {
         User_ID: app.Data.userInfo.User_ID,
         Amount: money,
         Note: '余额充值'
      }
      co.getShareApi('user/Fmg/moneyRechargeConsume', param, function(res) {
         if (res.code == 0) {
            app.getWXUserInfo(function(res) {

            })
         } else {
            console.log("充值失败")
         }
      })
   },
})